﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using Easy.BLL;
using Easy.Operation.Data;

namespace Easy.Web.Pages.DataPages.Form
{
    public partial class FormPage : BasePage
    {
        public string FormHtml = "";
        public string FormButton = "";
        protected string entityName;
        protected string formName;

        protected void Page_Load(object sender, EventArgs e)
        {
            string FormSet = Request.Params["FormSet"] == null ? "DefaultForm" : Request.Params["FormSet"];
            formName = Request.Params["FormSet"] == null ? "DefaultForm" : Request.Params["FormSet"];
            entityName = Context.Request["entityname"].ToLower();
            PageForm form = new PageForm(this.Context, this.GetSystemUser());
            if (form.GetFormStructure(FormSet) != null)
            {
                FormHtml = form.GetFormStructure(FormSet)[0];
                FormButton = form.GetFormStructure(FormSet)[1];
                //添加JS引用
                //string JS = @"<script src='" + form.GetFormStructure(FormSet)[2] + "'></script>";
                string JS=String.Empty;
                //处理beforeload事件
                JS += "<script>";
                JS += "if(((" + (string.IsNullOrEmpty(form.GetFormStructure(FormSet)[3]) ? "function(){ return true }" : form.GetFormStructure(FormSet)[3]) + ")()) == false ? false : true){ ";
                //处理beforesave,,aftersave事件
                JS += "var SaveData = function(fun){ core.Util.SaveData(fun,(" + (string.IsNullOrEmpty(form.GetFormStructure(FormSet)[5]) ? "null" : form.GetFormStructure(FormSet)[5]) + "),(" + (string.IsNullOrEmpty(form.GetFormStructure(FormSet)[6]) ? "null" : form.GetFormStructure(FormSet)[6]) + ")); };";
                JS += "core.Form.ValueForm();";
                JS += "core.Form.BindDef(); ";
                //处理afterload事件
                if (!string.IsNullOrEmpty(form.GetFormStructure(FormSet)[4]))
                {
                    JS += "(" + form.GetFormStructure(FormSet)[4] + ")();";
                }

                //afterload事件执行完成后显示页面
                JS += "$('#Left').height($(window).height() - 50).show();";
                JS += "$('#Content').height($(window).height() - 50).show();";
                JS += "$('._FormContainer').height($(window).height() - 50).show();";
                JS += "$('._EntityLinkContainer').height($(window).height() - 50).show();";
                JS += "$('#Top').show();";
                JS += "$('#BackGroundMessage').hide();";

                JS += "}</script>";
                this.FormJS.Text = JS;


            }
            this.CoreScript.Text = string.Format(@"<script>
                                                    var EntityName='{0}';
                                                    var EntityDisplayName='{1}';
                                                    var Prefix='{2}';
                                                    var FormId='{3}';
                                                    var XFormData={4};
                                                    var EntityField={5};
                                                    var EntityOption={6};
                                                    var Privilege={7};
                                                    var FunctionPrivilege={8};
                                                    var Attachments={9};
                                                    {10}
                                                    </script>",
                                                              form.EntityName,
                                                              form.EntityDispalayName,
                                                              form.PreFix,
                                                              form.ID,
                                                              JsonConvert.SerializeObject(form.EntityData),
                                                              JsonConvert.SerializeObject(form.Fields),
                                                              JsonConvert.SerializeObject(form.Options),
                                                              JsonConvert.SerializeObject(form.UserPrivilege),
                                                              JsonConvert.SerializeObject(form.FunctionPrivilege),
                                                              JsonConvert.SerializeObject(form.GetAttachments()),
                                                              Request["_content"] == null ? "var __Def=null;" : "var __Def='" + Request["_content"] + "';");
        }
    }
}